import os
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.patches as patches
from matplotlib.font_manager import FontProperties
from matplotlib import gridspec

fs = 14
font = {'family' : 'times new roman',
        'weight' : 'normal',  
        'size'   : fs,  
        }

numHosts  = 10
numToRs   = 100
numCores  = 4
numOCSes  = 4
numDemands = 1
flowsNum_b = 1

flowSizes   = [(250, 250)]
groupSizes  = [(3,3), (5,5), (7,7), (9,9), (11,11), (13,13), (15,15), (17,17), (19,19), (21,21)] # 10, 20, 30, 40, 50, 60, 70, 80, 90, 100
scheduler   = ['unicast', 'binomial', 'ring', 'EPS', 'OCS', 'Jcast']
update_enable = True

x           = []
y_unicast   = []
y_binomial  = []
y_ring      = []
y_EPS       = []
y_OCS       = []
y_Jcast     = []

for i in groupSizes:
    x.append((i[0]-1)*5)

for (groupSize_a, groupSize_b) in groupSizes: # different groupSize
    for (flowSize_a, flowSize_b) in flowSizes: # different flowSize
        for sched in scheduler:
            mflowsFileName = "%sCore_%sOCS_%sToR_%sHost_%sDemand_%sFlows_%s%sSize_%s%sGroup_%s_%s.txt"\
                        %(numCores, numOCSes, numToRs, numHosts, numDemands, flowsNum_b, flowSize_a, flowSize_b, groupSize_a, groupSize_b, sched, update_enable)

            if os.path.isfile(mflowsFileName):
                with open(mflowsFileName, 'r') as f:
                    for line in f.readlines():
                        split = line.split()
                        fct = float(split[5])
                        if sched == 'unicast':
                            y_unicast.append(fct)
                        elif sched == 'binomial':
                            y_binomial.append(fct)
                        elif sched == 'ring':
                            y_ring.append(fct)
                        elif  sched == 'EPS':
                            y_EPS.append(fct)
                        elif  sched == 'OCS':
                            y_OCS.append(fct)
                        elif sched == 'Jcast':
                            y_Jcast.append(fct)

fig = plt.figure(figsize=(6,3.8))
marker_set = ['+', 's', 'o', 'x', '<', 'D']
markers = {}
for i in range(0, len(scheduler)):
    markers[scheduler[i]] =  marker_set[i]

lw = 1
ms = 6
plt.plot(x, y_Jcast, 'D', color= 'y', ls='-.', lw=lw, ms=ms, zorder=3)
plt.plot(x, y_OCS, '<', color= 'r', ls='-.', lw=lw, ms=ms+2)
plt.plot(x, y_EPS, 'x', color= 'blue', ls='-.', lw=lw, ms=ms+2,mew=2)
plt.plot(x, y_ring, 'o', color= 'purple', ls='-.', lw=lw, ms=ms)
plt.plot(x, y_binomial, 's',color= 'seagreen', ls='-.', lw=lw, ms=ms)
plt.plot(x, y_unicast, '+', color='black', ls='-.', lw=lw, ms=ms+2, mew=2)

plt.subplots_adjust(bottom=0.14, top=0.9, left=0.12, right=0.95)
plt.yscale('log')
plt.xlim(5, 105)
plt.xlabel('Multicast Group Size', fontdict=font)
plt.ylabel('Multicast Flow Completion Time (s)', fontdict=font)
plt.xticks(fontproperties="times new roman", fontsize=fs)
plt.yticks(fontproperties='times new roman')
plt.text(70, 70, 'Flow Size = 250MB', fontdict = font, backgroundcolor='w')
plt.legend( ('Jcast', 'OCS', 'EPS', 'Ring', 'Binomial Tree', 'Naive Unicast'), ncol=2, \
            prop={'family':'times new roman', 'size': 11}, loc=2) #loc=2, fontsize=fs

plt.grid(ls='--')
plt.savefig('0_Simulation_GroupSize_FCT.pdf')
plt.show()